package invoicesystem.dao.impl;

import invoicesystem.dao.OrderDao;
import invoicesystem.entry.Order;
import invoicesystem.pojo.OrderForm;
import invoicesystem.util.DBHelper;

import java.util.ArrayList;

public class OrderDaoImpl implements OrderDao {

    private DBHelper db = new DBHelper();
    @Override
    public ArrayList selectSum(Integer enterprise_id) {
        String sql = "select * from `order` where enterprise_id=? ";
        return db.selectinfo(sql, Order.class,enterprise_id);
    }

    @Override
    public ArrayList<Order> selectOrderByNoOrTotal_amountOrCreate_time(Order order) {
        return null;
    }
    @Override
    public ArrayList<Order> selectByidAndSttausA(int eid) {
        String sql="select * from `order` where invoicing_status='未开票' AND enterprise_id=?";
        return  db.selectinfo(sql,Order.class,eid);
    }
    @Override
    public ArrayList<Order> selectByidAndSttausB(int eid) {
        String sql="select * from `order` where invoicing_status='已开票' AND enterprise_id=?";
        return  db.selectinfo(sql,Order.class,eid);
    }

    @Override
    public ArrayList<Order> selectAll(Order order, OrderForm orderForm) {
        StringBuilder sql=new StringBuilder("select * FROM `order` where 1=1 ");
        ArrayList list=new ArrayList();
        if (order.getNo()!=null){
            sql.append(" and no like ? ");
            list.add("%"+order.getNo()+"%");
        }
        if(order.getCreate_time()!=null){
            sql.append(" and create_time like ? ");
            list.add("%"+order.getCreate_time()+"%");
        }
        if(orderForm.getMinTotal_amount()!=null){
            sql.append(" and total_amount >= ? ");
            list.add(orderForm.getMinTotal_amount());
        }
        if(orderForm.getMaxTotal_amount()!=null){
            sql.append(" and total_amount <= ? ");
            list.add(orderForm.getMaxTotal_amount());

        }
        return db.selectinfo(sql.toString(),Order.class,list.toArray());
    }

    @Override
    public boolean updateOrder(int enterprise_id, String no) {
        String sql="update `order` set invoicing_status='未开票' where enterprise_id=? and no=? invoicing_status='已开票' ";
        return db.updateinfo(sql,enterprise_id,no)>0?true:false;
    }

    @Override
    public boolean updateOrderno(String no) {
        String sql="update `order` set invoicing_status='已开票' where no=? ";
        System.out.println(no);
        return db.updateinfo(sql,no)>0?true:false;
    }

}
